package com.guigu.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.SysMenuRoleMapper;
import com.guigu.common.mapper.SysMenusMapper;
import com.guigu.common.mapper.SysRolesMapper;
import com.guigu.common.mapper.SysUserRoleMapper;
import com.guigu.common.pojo.SysMenus;
import com.guigu.lc.serach.MenusSerach;
import com.guigu.lc.serach.RolesSerach;
import com.guigu.lc.service.sys_RoleService;
import com.guigu.lc.view.RoleandMenuview;
import com.guigu.lc.view.Roleview;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class sys_RoleServiceimpl implements sys_RoleService {

    @Resource
    private SysRolesMapper rolesMapper;

    @Resource
    private SysMenusMapper menusMapper;

    @Resource
    private SysMenuRoleMapper sysMenuRoleMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public RolesSerach queryallrolesyService(RolesSerach rolesSerach) {
        PageHelper.startPage(rolesSerach.getPageNumber(),rolesSerach.getPageSize());
        PageInfo page=new PageInfo(this.rolesMapper.queryallroles(rolesSerach));
        RolesSerach roeserachx=new RolesSerach();
        roeserachx.setRows(page.getList());
        roeserachx.setTotal(page.getTotal());
        return roeserachx;
    }

    @Override
    public MenusSerach querygrentrolemenusService(MenusSerach roleandMenuview) {
        PageHelper.startPage(roleandMenuview.getPageNumber(),roleandMenuview.getPageSize());
        PageInfo page=new PageInfo(this.menusMapper.querymenuByuserid(roleandMenuview));
        MenusSerach view=new MenusSerach();
        view.setRows(page.getList());
        view.setTotal(page.getTotal());
        return view;
    }

    @Override
    public RoleandMenuview targtentidService(int id) {
        List<SysMenus> sysMenus = this.menusMapper.querygrentByroleid(id);
        StringBuffer targetrolestr=new StringBuffer();
        for(SysMenus t:sysMenus) {
            targetrolestr.append(t.getId()).append(",");
        }
        RoleandMenuview view=new RoleandMenuview();
        view.setX(targetrolestr);
        view.setTarroleid(id);
        return view;
    }

    @Override
    public void grentshouhuirolemenus(RoleandMenuview roleandMenuview) {
        this.sysMenuRoleMapper.deleterolemenuByroleidand(roleandMenuview);
        this.sysMenuRoleMapper.insertrolemenuByroleidand(roleandMenuview);
    }

    @Override
    public void grentshouhuirolemenus2(RoleandMenuview roleandMenuview) {
        this.sysMenuRoleMapper.deleterolemenuByroleidand(roleandMenuview);
    }

    @Override
    public void insertaddandupdateroleuserService(Roleview roles) {
       this.rolesMapper.insertrolesyszc(roles.getRoles());
        this.sysUserRoleMapper.inserroleuserloginyonghu(roles);
    }
}
